package net.wanho.service.impl;

import net.wanho.po.Attribute;
import net.wanho.mapper.AttributeMapper;
import net.wanho.service.AttributeService;
import org.springframework.stereotype.Service;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


/**
 * <p>
 * 属性表 服务实现类
 * </p>
 *
 * @author 汤小洋
 * @since 2025-09-25
 */
@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class AttributeServiceImpl implements AttributeService {

    private final AttributeMapper attributeMapper;

    /**
     * 属性表分页查询
     * @param pageNum
     * @param pageSize
     * @param attribute
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public PageInfo findPage(int pageNum, int pageSize, Attribute attribute) {
        if(ObjectUtil.isNull(attribute)){
            attribute = new Attribute();
        }

        // 分页
        PageHelper.startPage(pageNum,pageSize);
        List<Attribute> attributeList = attributeMapper.select(attribute);
        return new PageInfo(attributeList);
    }

    /**
     * 根据主键查询属性表
     * @param attrId
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public Attribute findById(Serializable attrId) {
        return attributeMapper.selectById(attrId);
    }

    /**
     * 属性表新增
     * @param attribute
     */
    @Override
    public void add(Attribute attribute) {
        attributeMapper.insert(attribute);
    }

    /**
     * 修改属性表
     * @param attribute
     */
    @Override
    public void modify(Attribute attribute) {
        attributeMapper.update(attribute);
    }

    /**
     * 根据主键删除属性表
     * @param attrId
     */
    @Override
    public void removeById(Serializable attrId) {
        if(((String)attrId).contains(",")){
            attributeMapper.deleteByIds(((String)attrId).split(","));
        }else {
            attributeMapper.deleteById(attrId);
        }
    }
}
